package com.lxy.digitaltwins.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxy.digitaltwins.common.domain.R;
import com.lxy.digitaltwins.domain.po.Equipment;
import com.lxy.digitaltwins.domain.vo.DataVO;
import com.lxy.digitaltwins.domain.vo.ThermometerVO;
import com.lxy.digitaltwins.mapper.EquipmentMapper;
import com.lxy.digitaltwins.properties.IoTDBProperties;
import com.lxy.digitaltwins.service.EquipmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.iotdb.isession.SessionDataSet;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.tsfile.read.common.Field;
import org.apache.tsfile.read.common.RowRecord;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {
    private final IoTDBProperties ioTDBProperties;

    @Override
    public ThermometerVO getThermometer() throws Exception {
        //IoTDB数据库建立连接
        Session session=new Session.Builder()
                .host(ioTDBProperties.getHost())
                .port(ioTDBProperties.getPort())
                .username(ioTDBProperties.getUserName())
                .password(ioTDBProperties.getPassWord())
                .build();
        try{
            session.open(false);
            session.setFetchSize(10000);
            //查找数据
            String sql = "select * from root.transformerSubstation.Thermometer order by time desc limit 1";
            SessionDataSet dataSet = session.executeQueryStatement(sql);
            RowRecord rowRecord = dataSet.next();
            String temp = rowRecord.getFields().get(0).getStringValue();
            ThermometerVO thermometerVO=new ThermometerVO();
            thermometerVO.setName("root.transformerSubstation.Thermometer");
            thermometerVO.setTemp(temp);
            session.close();
            return thermometerVO;
        }catch (Exception e){
            throw new Exception("IoTDB发送数据错误");
        }

        }

    @Override
    public DataVO getSensorDate(String name) throws Exception {
        //分割设备和传感器
        String device="";
        String sensor="";
        int lastDotIndex = name.lastIndexOf('.');
        // 截取最后一部分（点之后的内容）
        sensor= name.substring(lastDotIndex + 1);
        // 截取点之前的所有内容
        device = name.substring(0, lastDotIndex);
        //查询数据
        //IoTDB数据库建立连接
        Session session=new Session.Builder()
                .host(ioTDBProperties.getHost())
                .port(ioTDBProperties.getPort())
                .username(ioTDBProperties.getUserName())
                .password(ioTDBProperties.getPassWord())
                .build();
        try{
            session.open(false);
            session.setFetchSize(10000);
            //查找数据
            String sql = "select "+sensor+" from "+device+" order by time desc limit 1";
            SessionDataSet dataSet = session.executeQueryStatement(sql);
            RowRecord rowRecord = dataSet.next();
            String data = rowRecord.getFields().get(0).getStringValue();
            DataVO dataVO=new DataVO();
            dataVO.setName(name);
            dataVO.setData(data);
            session.close();
            return dataVO;
        }catch (Exception e){
            throw new Exception("IoTDB发送数据错误");
        }
    }
}
